﻿using UnityEngine;
using System.Collections;

public class PressSkillBar : uiBaseButton {

    const float ClosePosx = -224.5f;
	public bool isOpen;
	public bool isFriendIncluded;
	bool isMoving;
	
	public GameObject [] skillIcons;
	public int [] skillCost;
	//public UILabel [] itemCount;
	
	public bool isDisableClick;
	
	void Awake()
	{
		skillCost = new int[4];
	}
	
	void OnClick()
	{
		if(isDisableClick)
			return;
		
		if(isMoving)
			return;
		isOpen = !isOpen;
		Vector3 destPos = Vector3.zero;
		isMoving = true;
		if(isOpen)
		{
            //if(!isFriendIncluded)
            //    destPos.x = -245.3397f;
			iTween.MoveTo( gameObject , iTween.Hash("time", 0.7f, "position", destPos,"oncomplete","MoveDone",    "islocal", true, "easetype","easeOutExpo" )  );	
		}
		else
		{
            destPos.x = ClosePosx;
			iTween.MoveTo( gameObject , iTween.Hash("time", 0.7f, "position", destPos,"oncomplete","MoveDone",     "islocal", true, "easetype","easeOutExpo" )  );
		}
	}
	
	public void DisableSkillBar(bool bDisable)
	{
		isDisableClick = bDisable;
		if(bDisable)
			StartCoroutine("CoCloseBar");
	}
	
//	public void CloseBar()
//	{
//		StartCoroutine("CoCloseBar");
//	}
//	
	IEnumerator CoCloseBar()
	{
		while(isMoving)
		{
			yield return null;
		}
		
		if(isOpen)
		{
			isOpen = false;
			Vector3 destPos = Vector3.zero;
            destPos.x = ClosePosx;
			//gameObject.transform.localPosition = destPos;
			iTween.MoveTo( gameObject , iTween.Hash("time", 0.7f, "position", destPos,"oncomplete","MoveDone",     "islocal", true, "easetype","easeOutExpo" )  );			
		}
	}
	
	void MoveDone()
	{
		isMoving = false;
		glog.debug("skillbar move done");
	}
	
	public void SetItemCount(int hp, int res)
	{
        //itemCount[0].text = "x"+hp.ToString();
        //itemCount[1].text = "x"+res.ToString();
	}
	
	public void SetSkillData(int cost, string iconName, int index)
	{      
		skillIcons[index].GetComponent<UISprite>().spriteName = iconName;
		skillCost[index] = cost;
	}
	
	public void UpdateSkillPressBar(int mp)
	{
		for(int i=0; i<4; i++)
		{
			if( skillCost[i] > mp)
				SetDisable(true, i);
		}
	}
	
	public void SetActivate(bool bActivate, int index)
	{
		skillIcons[index].SetActive(bActivate);
        UISprite BG = skillIcons[index].transform.parent.GetComponent<UISprite>();

        Color color = BG.color;
        
        if (bActivate)
            color.a = 1.0f;
        else
            color.a = 0.5f;

        BG.color = color;

        //if(bActivate)
        //{
        //    if(index==2 || index==3)
        //        isFriendIncluded = true;
        //}
	}
	
	public void SetDisable(bool bDisable, int index)
	{
        Color color = skillIcons[index].GetComponent<UISprite>().color;
        if (bDisable)
        {
            color.r = 81 / 255.0f;
            color.g = 81 / 255.0f;
            color.b = 81 / 255.0f;
        }
        else
        {
            color = Color.white;
        }
        skillIcons[index].GetComponent<UISprite>().color = color;
	}
	
}
